<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="couponId">
      <el-input v-model="dataForm.couponId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="couponNumber">
      <el-input v-model="dataForm.couponNumber" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="userId">
      <el-input v-model="dataForm.userId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="usedTime">
      <el-input v-model="dataForm.usedTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="addTime">
      <el-input v-model="dataForm.addTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="来源key" prop="sourceKey">
      <el-input v-model="dataForm.sourceKey" placeholder="来源key"></el-input>
    </el-form-item>
    <el-form-item label="发券人" prop="referrer">
      <el-input v-model="dataForm.referrer" placeholder="发券人"></el-input>
    </el-form-item>
    <el-form-item label="状态 1. 可用 2. 已用 3. 过期" prop="couponStatus">
      <el-input v-model="dataForm.couponStatus" placeholder="状态 1. 可用 2. 已用 3. 过期"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          couponId: '',
          couponNumber: '',
          userId: '',
          usedTime: '',
          addTime: '',
          orderId: '',
          sourceKey: '',
          referrer: '',
          couponStatus: ''
        },
        dataRule: {
          couponId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          couponNumber: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          usedTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          addTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          orderId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sourceKey: [
            { required: true, message: '来源key不能为空', trigger: 'blur' }
          ],
          referrer: [
            { required: true, message: '发券人不能为空', trigger: 'blur' }
          ],
          couponStatus: [
            { required: true, message: '状态 1. 可用 2. 已用 3. 过期不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/platform/usercoupon/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.couponId = data.usercoupon.couponId
                this.dataForm.couponNumber = data.usercoupon.couponNumber
                this.dataForm.userId = data.usercoupon.userId
                this.dataForm.usedTime = data.usercoupon.usedTime
                this.dataForm.addTime = data.usercoupon.addTime
                this.dataForm.orderId = data.usercoupon.orderId
                this.dataForm.sourceKey = data.usercoupon.sourceKey
                this.dataForm.referrer = data.usercoupon.referrer
                this.dataForm.couponStatus = data.usercoupon.couponStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/platform/usercoupon/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'couponId': this.dataForm.couponId,
                'couponNumber': this.dataForm.couponNumber,
                'userId': this.dataForm.userId,
                'usedTime': this.dataForm.usedTime,
                'addTime': this.dataForm.addTime,
                'orderId': this.dataForm.orderId,
                'sourceKey': this.dataForm.sourceKey,
                'referrer': this.dataForm.referrer,
                'couponStatus': this.dataForm.couponStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
